package com.wz.project.pda.controller;

import com.wz.framework.aspectj.lang.annotation.Log;
import com.wz.framework.aspectj.lang.enums.BusinessType;
import com.wz.framework.web.controller.BaseController;
import com.wz.framework.web.domain.AjaxResult;
import com.wz.framework.web.page.TableDataInfo;
import com.wz.project.api.barcode.domain.Barcode;
import com.wz.project.api.barcode.domain.BarcodeInfo;
import com.wz.project.api.barcode.domain.MatnrInfo;
import com.wz.project.api.barcode.service.BarcodeService;
import com.wz.project.api.sap.controller.SapController;
import com.wz.project.api.tmsMatnr.domain.TmMatnr;
import com.wz.project.api.tmsMatnr.service.ITmMatnrService;
import com.wz.project.barcodeMsg.domain.BarcodeMsg;
import com.wz.project.barcodeMsg.service.IBarcodeMsgService;
import com.wz.project.bas.product.domain.Product;
import com.wz.project.bas.product.service.IProductService;
import com.wz.project.inStock.stockindetail.domain.InStockinDetail;
import com.wz.project.mydo.domain.MydoJob;
import com.wz.project.mydo.domain.MydoJobDetail;
import com.wz.project.mydo.mydojob.service.IMydoJobService;
import com.wz.project.mydo.mydojobdetail.service.IJobDetailService;
import com.wz.project.outStock.stockout.domain.OutStockout;
import com.wz.project.outStock.stockout.service.IOutStockoutService;
import com.wz.project.outStock.stockoutdetail.domain.OutStockoutDetail;
import com.wz.project.outStock.stockoutdetail.service.IOutStockoutDetailService;
import com.wz.project.outTray.outTray.domain.OutTrayMsg;
import com.wz.project.outTray.outTray.service.IOutTrayMsgService;
import com.wz.project.pda.service.PdaService;
import com.wz.project.pda.service.PdaServicePk;
import com.wz.project.sap.DN.domain.ZflWmsGetDn;
import com.wz.project.sap.DN.service.IZflWmsGetDnService;
import com.wz.project.sap.DN_POST_DN.domain.ZflWmsDnPostDn;
import com.wz.project.sap.DN_POST_DN.service.IZflWmsDnPostDnService;
import com.wz.project.stock.domain.BasStock;
import com.wz.project.stock.stockdetail.domain.BasStockDetail;
import com.wz.project.stock.stockdetail.service.IBasStockDetailService;
import com.wz.project.surebarcode.sureBarcode.domain.Surebarcode;
import com.wz.project.surebarcode.sureBarcode.service.ISurebarcodeService;
import com.wz.project.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/pda")
public class PdaController extends BaseController {

    private String prefix = "rf";


    @Autowired
    PdaServicePk pdaService;
    @Autowired
    IZflWmsDnPostDnService zflWmsDnPostDnService;
    @Autowired
    private IOutTrayMsgService outTrayMsgService;
    @Autowired
    private ITmMatnrService tmMatnrService;

    @Autowired
    private IJobDetailService jobDetailService;

    @Autowired
    private IMydoJobService mydoJobService;


    @GetMapping("/productsh")
    public String productsh() {
        return prefix + "/productsh";
    }

    /**
     * 配套入立库
     * @return
     */
    @GetMapping("/productIn")
    public String productIn() {
        return prefix + "/productIn";
    }
    /**
     * 配套入平库
     * @return
     */
    @GetMapping("/productInP")
    public String productInP() {
        return prefix + "/productInP";
    }

    /**
     * 出库确认
     * @return
     */
    @GetMapping("/productCKsure")
    public String productCKsure() {
        return prefix + "/productCKsure";
    }


    @GetMapping("/barcodeget")
    public String barcodeget() {
        return prefix + "/barcodeget";
    }

    @GetMapping("/recover")
    public String recover() {
        return prefix + "/recover";
    }
    /**
     * 质检
     * @return
     */
    @GetMapping("/productzj")
    public String DN_POST_DN() {
        return prefix + "/productzj";
    }
    /**
     * 生产入立库
     * @return
     */
    @GetMapping("/productInLKCP")
    public String productInLKCP() {
        return prefix + "/productInLKCP";
    }
    @GetMapping("/productInLKCPDJ")
    public String productInLKCPDJ() {
        return prefix + "/productInLKCPDJ";
    }
    /**
     * 空托盘入库
     * @param basStock
     * @return
     */
    @RequestMapping("/jobTpAdd")
    @ResponseBody
    @Log(title = "空托盘入库", businessType = BusinessType.INSERT)
    public AjaxResult jobTpAdd( @RequestBody BasStock basStock) {
        AjaxResult ajaxResult = pdaService.jobTpAdd(basStock.getTrayCode(), basStock.getQuantity().intValue());
        return ajaxResult;
    }
    @RequestMapping("/jobTpAdd1")
    @ResponseBody
    @Log(title = "空托盘入库", businessType = BusinessType.INSERT)
    public AjaxResult jobTpAdd1(BasStock basStock) {
        AjaxResult ajaxResult = pdaService.jobTpAdd(basStock.getTrayCode(), basStock.getQuantity().intValue());
        return ajaxResult;
    }
    /**
     * 空托盘出库
     * @param basStock
     * @return
     */
    @RequestMapping("/jobTpOut")
    @ResponseBody
    @Log(title = "空托盘出库", businessType = BusinessType.INSERT)
    public AjaxResult jobTpOut( @RequestBody BasStock basStock) {
        AjaxResult ajaxResult = pdaService.jobTpOut(basStock.getLineId(),basStock.getQuantity().intValue());
        return ajaxResult;
    }
    /**
     * 条码采集
     * @param barcodeMsg
     * @return
     */
    @RequestMapping("/barcodesave")
    @ResponseBody
    @Log(title = "条码采集", businessType = BusinessType.INSERT)
    public AjaxResult barcodesave(@RequestBody BarcodeMsg barcodeMsg) {
        return pdaService.barcodeMsgAdd(barcodeMsg);
    }

    /**
     * 扫描新增库存
     * @param basStock
     * @return
     */
    @RequestMapping("/stockSave")
    @ResponseBody
    @Log(title = "扫描新增库存", businessType = BusinessType.INSERT)
    public AjaxResult stockSave( @RequestBody BasStock basStock) {
        AjaxResult ajaxResult = pdaService.addStock(basStock.getTrayCode(), basStock.getCargoSpaceId(), basStock.getBarcode());
        return ajaxResult;

    }
    /**
     * 收货确认数据获取
     * @param tmMatn
     * @return
     */
    @RequestMapping("/tms/listSH")
    @ResponseBody
    @Log(title = "条码扫描收货", businessType = BusinessType.INSERT)
    public Map selectTmsMsgSH(@RequestBody  TmMatnr tmMatn ) {
        return pdaService.selectTmsMsgSH(tmMatn);
    }

    /**
     * 确认收货
     * @param tmMatn
     * @return
     */
    @RequestMapping("/SureSH")
    @ResponseBody
    @Log(title = "收货确认", businessType = BusinessType.UPDATE)
    public AjaxResult SureSH( @RequestBody  TmMatnr tmMatn ) {
        return pdaService.sureSH(tmMatn);
    }
    /**
     * 确认收货
     * @param tmMatn
     * @return
     */
    @RequestMapping("/SureSH1")
    @ResponseBody
    @Log(title = "收货确认", businessType = BusinessType.UPDATE)
    public AjaxResult SureSH1(TmMatnr tmMatn ) {
        return pdaService.sureSH(tmMatn);
    }
    /**
     * 质检信息查询
     * @param zflWmsDnPostDn
     * @return
     */
    @RequestMapping("/list1")
    @ResponseBody
    @Log(title = "质检查询", businessType = BusinessType.OTHER)
    public TableDataInfo list1(@RequestBody ZflWmsDnPostDn zflWmsDnPostDn) {

        if (zflWmsDnPostDn.getVBELN() != null && !"".equals(zflWmsDnPostDn.getVBELN())) {
            zflWmsDnPostDn.setVBELN(String.format("%010d", Long.parseLong(zflWmsDnPostDn.getVBELN())));
        } else {
            zflWmsDnPostDn.setVBELN(String.format("%010d", 0));
        }
        startPage();
        List<ZflWmsDnPostDn> list = zflWmsDnPostDnService.selectZflWmsDnPostDnList1(zflWmsDnPostDn);
        return getDataTable(list);
    }
    /**
     * 质检
     * @param zflWmsDnPostDn1
     * @return
     */
    @RequestMapping("/check")
    @ResponseBody
    @Log(title = "质检确认", businessType = BusinessType.UPDATE)
    public AjaxResult check(@RequestBody ZflWmsDnPostDn zflWmsDnPostDn1) {
        String prueflos=zflWmsDnPostDn1.getPRUEFLOS();String type = zflWmsDnPostDn1.getTYPE();
        AjaxResult ajaxResult = new AjaxResult();

        ZflWmsDnPostDn zflWmsDnPostDn = zflWmsDnPostDnService.selectZflWmsDnPostDisticnctByprueflos(prueflos);
        if (zflWmsDnPostDn != null && "0".equals(zflWmsDnPostDn.getTYPE())) {
            if ("OK".equals(type)) {
                zflWmsDnPostDnService.updateZflWmsDnPostDnZjOk(prueflos);
            } else {
                zflWmsDnPostDnService.updateZflWmsDnPostDnZjNo(prueflos);
            }
            ajaxResult.put("status", "操作成功");
        } else {
            ajaxResult.put("status", "该质检已完成无需重复操作");
        }
        return ajaxResult;
    }

    /**
     * 出库分配
     * @param tmMatnr
     * @return
     */
    @RequestMapping("/autoAllocation")
    @ResponseBody
    @Log(title = "出库分配", businessType = BusinessType.INSERT)
    public Map<String, Object> autoAllocation(@RequestBody  TmMatnr tmMatnr) {
        return pdaService.autoAllocation(tmMatnr);
    }
    /**
     *
     * @param outTrayMsg
     * @return
     */
    @RequestMapping("/listoutTrayMsg")
    @ResponseBody
    @Log(title = "出库信息查询", businessType = BusinessType.OTHER)
    public TableDataInfo list(@RequestBody OutTrayMsg outTrayMsg)
    {
        if (outTrayMsg.getStockoutGuid() != null && !"".equals(outTrayMsg.getStockoutGuid())) {
            outTrayMsg.setStockoutGuid(String.format("%010d", Long.parseLong(outTrayMsg.getStockoutGuid())));
        } else {
            outTrayMsg.setStockoutGuid(String.format("%010d", 0));
        }
        startPage();
        List<OutTrayMsg> list = outTrayMsgService.selectOutTrayMsgList(outTrayMsg);
        return getDataTable(list);
    }
    /**
     * 确认出库
     * @param tmMatn
     * @return
     */
    @RequestMapping("/sureOut")
    @ResponseBody
    @Log(title = "出库确认", businessType = BusinessType.UPDATE)
    public AjaxResult sureOut(@RequestBody  TmMatnr tmMatn) {
        return pdaService.sureOut(tmMatn);
    }

    @RequestMapping("/listTmMatnr")
    @ResponseBody
    public TableDataInfo list1(@RequestBody TmMatnr tmMatnr)
    {
        startPage();
        tmMatnr.setFid(tmMatnr.getInStockId());
        tmMatnr.setInStockId(tmMatnr.getInStockId().split("-")[0]);

        List<TmMatnr> list = tmMatnrService.selectTmMatnrList(tmMatnr);
        return getDataTable(list);
    }
    /**
     * 配套入库
     *
     * @param basStockDetail
     * @return
     */
    @RequestMapping("/tms/listCg")
    @ResponseBody
    @Log(title = "配套上架信息查询", businessType = BusinessType.OTHER)
    public Map selectTmsMsgCg(@RequestBody BasStockDetail basStockDetail) {

        return pdaService.selectTmsMsgCg(basStockDetail);
    }

    //删除子件
    @RequestMapping("/tms/shanchu")
    @ResponseBody
    public Map shanchu(BasStockDetail barcodeDetail) {
        HashMap map = new HashMap();
        int i = jobDetailService.deleteJobDetailByshanchu(barcodeDetail.getProductId(), barcodeDetail.getBarcodeDetail());
        List<MydoJobDetail> mydoJobDetails = jobDetailService.selectJobDetailByjobid(barcodeDetail.getJobId());
        if (mydoJobDetails.size()==0){
            mydoJobService.deleteJobByIds(barcodeDetail.getJobId());
        }
        map.put("msg",i);
        return map;
    }

    @RequestMapping("/tms/listCgw")
    @ResponseBody
    @Log(title = "配套上架信息查询", businessType = BusinessType.OTHER)
    public Map selectTmsMsgCg1(BasStockDetail basStockDetail) {

        return pdaService.selectTmsMsgCg(basStockDetail);
    }
    @RequestMapping("/saveJobw")
    @ResponseBody
    @Log(title = "立库确认上架信息", businessType = BusinessType.INSERT)
    public AjaxResult saveJobW(Inner inner) {
        AjaxResult ajaxResult = pdaService.insetMydoJob(inner.getTrayCode(), inner.getCargoSpaceId(), inner.getFbarcode());
        return ajaxResult;

    }
    @RequestMapping("/saveJob")
    @ResponseBody
    @Log(title = "立库确认上架信息", businessType = BusinessType.INSERT)
    public AjaxResult saveJob(@RequestBody  Inner inner) {
        AjaxResult ajaxResult = pdaService.insetMydoJob(inner.getTrayCode(), inner.getCargoSpaceId(), inner.getFbarcode());
        return ajaxResult;

    }
    @RequestMapping("/sureIn")
    @ResponseBody
    @Log(title = "立库确认上架完成", businessType = BusinessType.INSERT)
    public AjaxResult sureIn(@RequestBody  Inner inner) {
        String trayCode = inner.getTrayCode();
        int count = pdaService.selectJobByTray(trayCode, "0");
        if (count == 0) {
            return AjaxResult.success("1");
        }
        pdaService.updateJobStatus(trayCode);
        return AjaxResult.success("操作成功");
    }
    @RequestMapping("/sureInw")
    @ResponseBody
    @Log(title = "立库确认上架完成", businessType = BusinessType.INSERT)
    public AjaxResult sureInw(Inner inner) {
        String trayCode = inner.getTrayCode();
        int count = pdaService.selectJobByTray(trayCode, "0");
        if (count == 0) {
            return AjaxResult.success("无该托盘任务");
        }
        pdaService.updateJobStatus(trayCode);
        return AjaxResult.success("操作成功");
    }


    @RequestMapping("/saveJobP")
    @ResponseBody
    @Log(title = "确认上架信息", businessType = BusinessType.INSERT)
    public AjaxResult saveJobP(@RequestBody Inner inner) {
        AjaxResult ajaxResult = pdaService.insertMydoJobFinish(inner.getCargoSpaceId(), inner.getFbarcode(), inner.getQuantity().intValue(), inner.getTrayCode());
        return ajaxResult;
    }

    @RequestMapping("/sureInP")
    @ResponseBody
    @Log(title = "平库确认上架完成", businessType = BusinessType.INSERT)
    public AjaxResult sureInP(@RequestBody  Inner inner ) {
        String trayCode = inner.getTrayCode();
        int count = pdaService.selectJobByTray(trayCode, "0");
        if (count == 0) {
            return AjaxResult.success("未扫描信息");
        }
        pdaService.completeJob(trayCode);
        return AjaxResult.success("入库成功");
    }
    /**
     * 生产入库
     * @param basStockDetail
     * @return
     */
    @RequestMapping("/tms/listSc")
    @ResponseBody
    @Log(title = "生产上架信息查询", businessType = BusinessType.OTHER)
    public Map selectTmsMsgSc(@RequestBody BasStockDetail basStockDetail) {

        return pdaService.selectTmsMsgSc(basStockDetail);
    }
    /**
     * 生产入库
     * @param basStockDetail
     * @return
     */
    @RequestMapping("/tms/listScTray")
    @ResponseBody
    @Log(title = "已绑定托盘信息查询", businessType = BusinessType.OTHER)
    public Map selectMsgTrayCode(@RequestBody BasStockDetail basStockDetail) {

        return pdaService.selectMsgTrayCode(basStockDetail);
    }
    @RequestMapping("/tms/listSc1")
    @ResponseBody
    @Log(title = "生产上架信息查询", businessType = BusinessType.OTHER)
    public Map selectTmsMsgSc1(BasStockDetail basStockDetail) {

        return pdaService.selectTmsMsgSc(basStockDetail);
    }
    @RequestMapping("/tms/listScDj")
    @ResponseBody
    @Log(title = "生产上架信息查询(单据)", businessType = BusinessType.OTHER)
    public Map selectTmsMsgScDj(BasStockDetail basStockDetail) {

        return pdaService.selectTmsMsgScDj(basStockDetail);
    }
    /**
     * 生产入库
     * @param basStockDetail
     * @return
     */
    @RequestMapping("/tms/listScTray1")
    @ResponseBody
    @Log(title = "已绑定托盘信息查询", businessType = BusinessType.OTHER)
    public Map selectMsgTrayCode1(BasStockDetail basStockDetail) {
        return pdaService.selectMsgTrayCode(basStockDetail);
    }
    @RequestMapping("/saveJob1")
    @ResponseBody
    @Log(title = "立库确认上架信息", businessType = BusinessType.INSERT)
    public AjaxResult saveJob1(Inner inner) {
        AjaxResult ajaxResult = pdaService.insetMydoJob(inner.getTrayCode(), inner.getCargoSpaceId(), inner.getFbarcode());
        return ajaxResult;

    }
    @RequestMapping("/saveJobpk1")
    @ResponseBody
    @Log(title = "立库确认上架信息", businessType = BusinessType.INSERT)
    public AjaxResult saveJobpk1(Inner inner) {
        AjaxResult ajaxResult = pdaService.insetMydoJobcppk(inner.getTrayCode(), inner.getCargoSpaceId(), inner.getFbarcode(),inner.cargoSpaceId);
        return ajaxResult;

    }
    @RequestMapping("/sureIn1")
    @ResponseBody
    @Log(title = "立库确认上架完成", businessType = BusinessType.INSERT)
    public AjaxResult sureIn1(Inner inner) {
        String trayCode = inner.getTrayCode();
        int count = pdaService.selectJobByTray(trayCode, "0");
        if (count == 0) {
            return AjaxResult.success("1");
        }
        pdaService.updateJobStatus(trayCode);
        return AjaxResult.success("2");
    }
    @RequestMapping("/sureInpk1")
    @ResponseBody
    @Log(title = "平库确认上架完成", businessType = BusinessType.INSERT)
    public AjaxResult sureInpk1(Inner inner) {
        String trayCode = inner.getTrayCode();
        int count = pdaService.selectJobByTray(trayCode, "0");
        if (count == 0) {
            return AjaxResult.success("未扫描信息");
        }
        pdaService.completeJob(trayCode);
        return AjaxResult.success("入库成功");
    }


    //    @Scheduled(fixedRate = 30 * 1000)
    @RequestMapping("/finish_pk")
    @ResponseBody
    public void finishPK() {

        pdaService.finishPK();
    }
    @Scheduled(cron = "0 0 13 * * ?")
    @RequestMapping("/wms_data_in_del")
    @ResponseBody
    public void wms_data_in_del() {

        pdaService.wms_data_in_del();
    }
    @Scheduled(cron = "0 */1 * * *  ?")
    @RequestMapping("/wcs_mantr")
    @ResponseBody
    public void wcs_mantr() {

        pdaService.wcs_mantr();
    }
    static class Inner {
        private String fbarcode;
        private BigDecimal quantity;
        private String trayCode;
        private String cargoSpaceId;

        public String getCargoSpaceId() {
            return cargoSpaceId;
        }

        public void setCargoSpaceId(String cargoSpaceId) {
            this.cargoSpaceId = cargoSpaceId;
        }

        public String getFbarcode() {
            return fbarcode;
        }

        public void setFbarcode(String fbarcode) {
            this.fbarcode = fbarcode;
        }

        public BigDecimal getQuantity() {
            return quantity;
        }

        public void setQuantity(BigDecimal quantity) {
            this.quantity = quantity;
        }

        public String getTrayCode() {
            return trayCode;
        }

        public void setTrayCode(String trayCode) {
            this.trayCode = trayCode;
        }

        @Override
        public String toString() {
            return "Inner{" +
                    "fbarcode='" + fbarcode + '\'' +
                    ", quantity='" + quantity + '\'' +
                    ", trayCode='" + trayCode + '\'' +
                    '}';
        }
    }
}
